#include "VBackgroundWorker.h"

VBackgroundWorker::VBackgroundWorker(void) :
    _callback(NULL),
    _thread(NULL),
    _started(false)
{
}

VBackgroundWorker::~VBackgroundWorker(void)
{
}

bool VBackgroundWorker::start(void* data)
{
    if (_started) {
        return false;
    }

    _thread = SDL_CreateThread(_callback, data);

    if (_thread)
    {
        _started = true;
        return true;
    }

    return false;
}

void VBackgroundWorker::terminate()
{
    _started = false;
    SDL_KillThread(_thread);
}

int VBackgroundWorker::join()
{
    int status = 0;

    SDL_WaitThread(_thread, &status);

    return status;
}

unsigned int VBackgroundWorker::threadId() const
{
    return SDL_GetThreadID(_thread);
}

unsigned int VBackgroundWorker::currentThreadId()
{
    return SDL_ThreadID();
}

void VBackgroundWorker::setCallback(thread_callback_t callback)
{
    _callback = callback;
}

thread_callback_t VBackgroundWorker::getCallback(void) const
{
    return _callback;
}
